var imgFile = [];
var imgSrc = [];
var imgName = [];
var imgUploadUrl = [];
var imgBox = '.content-img-list';
$(function() {
	$('.content-img-list').on('mouseover', '.content-img-list-item', function() {
		$(this).children('div').removeClass('hide');
	});
	$('.content-img-list').on('mouseleave', '.content-img-list-item', function() {
		$(this).children('div').addClass('hide');
	});
	$(".content-img-list").on("click", '.content-img-list-item a .gcllajitong', function() {
		var index = $(this).parent().parent().parent().index();
		imgSrc.splice(index, 1);
		imgFile.splice(index, 1);
		imgName.splice(index, 1);
		imgUploadUrl.splice(index, 1);
		var boxId = ".content-img-list";
		addNewContent(boxId);
		if (imgSrc.length < 4) {
			$('.content-img .file').show();
		}
	});
	$(".content-img-list").on("click", '.content-img-list-item a .gclfangda', function() {
		var index = $(this).parent().parent().parent().index();
		$(".modal-content").html("");
		var bigimg = $(".modal-content").html();
		$(".modal-content").html(bigimg + '<div class="show"><img src="' + imgSrc[index] + '" alt=""><div>');
	});
});
$('#mainUpload').on('change', function(e) {
	var imgSize = this.files[0].size;
	if (imgSize > 1024 * 1024 * 20) {
		return alert("Upload images should not exceed 20M");
	};
	if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
		return alert("Incorrect picture upload format");
	}
	pushImg();
});
$('#upload').on('change', function(e) {
	if((imgSrc.length+this.files.length)>5){
		return alert("The number of images uploaded should not exceed 5");
	}
	var imgSize = this.files[0].size;
	if (imgSize > 1024 * 1024 * 20) {
		return alert("Upload images should not exceed 20M");
	};
	if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
		return alert("Incorrect picture upload format");
	}

	var fileList = this.files;

	var url = baseUrl+ "/admin/parameters/mainImgeUpload";
	var fd = new FormData();
	fd.append("uploadFile", fileList[0]);
	$.ajax({
		type: "POST",
		contentType:false, //必须false才会避开jQuery对 formdata 的默认处理 , XMLHttpRequest会对 formdata 进行正确的处理
		processData: false, //必须false才会自动加上正确的Content-Type
		url: url,
		data: fd,
		success: function (msg) {
			var obj = eval('(' + JSON.stringify(msg) + ')');
			if(obj.SUCESS){
				for (var i = 0; i < fileList.length; i++) {
					var imgSrcI = getObjectURL(fileList[i]);
					imgName.push(fileList[i].name);
					imgSrc.push(imgSrcI);
					imgFile.push(fileList[i]);
					imgUploadUrl.push(obj.DATA);
				}
				addNewContent(imgBox);
				this.value = null;
			}
		},
		error: function (msg) {
			//debugger;
			alert("error");
		}
	});


});
$('#btn-submit-upload').on('click', function() {
	var formFile = new FormData();
	$.each(imgFile, function(i, file) {
		formFile.append('myFile[]', file);
	});
});

function removeImg(obj, index) {
	imgSrc.splice(index, 1);
	imgFile.splice(index, 1);
	imgName.splice(index, 1);
	imgUploadUrl.splice(index, 1);
	var boxId = ".content-img-list";
	addNewContent(boxId);
}

function addNewContent(obj) {
	$(obj).html("");
	for (var a = 0; a < imgSrc.length; a++) {
		var oldBox = $(obj).html();
		$(obj).html(oldBox + '<li class="content-img-list-item"><img src="' + imgSrc[a] + '" alt="">' +
			'<div class="hide"><a index="' + a + '" class="delete-btn"><i class="gcl gcllajitong"></i></a>' +
			' </div></li>'
		);	
	}
}

function getObjectURL(file) {
	var url = null;
	if (window.createObjectURL != undefined) {
		url = window.createObjectURL(file);
	} else if (window.URL != undefined) {
		url = window.URL.createObjectURL(file);
	} else if (window.webkitURL != undefined) {
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}
